#!/usr/bin/perl

print"Content-type:text/html; charset=utf-8\n\n";
print"<title>WikiPage of SipTestTool</title><body>";

print "<a href='\/cgi/wiki.pl?..\/siptesttool.wiki/main.wiki'>Back To Main</a><p>";

$key="QUERY_STRING";
&open_file("INFILE", "", "$ENV{$key}");
#&open_file("INFILE", "", "../siptesttool.wiki/gettingstart.wiki");
$LineNo = 0;
$IsInOl=0;
$IsInOl2=0;
while ($line = &read_from_file("INFILE")) {
    $LineNo+=1;
#    print "$LineNo $line\n";
    if ($LineNo == 1){
    }
    else{
        $line =~ s/</&lt;/;
        $line =~ s/\{\{\{/<PRE>/; 
        $line =~ s/\}\}\}/<\/PRE>/; 
        $line =~ s/=== ([^ ]+) ===/<h3>$1<\/h3>/;
        $line =~ s/== ([^ ]+) ==/<h2>$1<\/h2>/;
        $line =~ s/= ([^ ]+) =/<h1>$1<\/h1>/;

        if ($line =~ /   [.]*/){
            if ($line =~ /   \#[.]*/){
                if ($IsInOl2 == 0){
                    $IsInOl2 = 1;
                    print "<ol>";
                }
                $line =~ s/ \#/<li>/
            }
        }
        else{
            if ($IsInOl2 == 1){
                $IsInOl2 = 0;
                print "</ol>"
            }
        }


        if ($line =~ / [.]*/){
            if ($line =~ / \#[.]*/){
                if ($IsInOl == 0){
                    $IsInOl = 1;
                    print "<ol>";
                }
                $line =~ s/ \#/<li>/
            }
        }
        else{
            if ($IsInOl == 1){
                $IsInOl = 0;
                print "</ol>"
            }
        }

        $line =~ s/^\#[^-]+//;
        $line =~ s/!([a-zA-Z]+)/$1/g;
        $line =~ s/http:\/\/siptesttool.googlecode.com\/svn\/wiki\/(ce[0-9]+\.png)/<img src="..\/siptesttool.wiki\/$1">/;
        $line =~ s/\n/<p>\n/;
        $line =~ s/\[([a-z_]+) ([^-]*)\]/<a href='\/cgi\/wiki.pl?..\/siptesttool.wiki\/$1.wiki'>$2<\/a>/;
        if ($line =~ /[^\.]+/){
            print "$line";
        }
    }
}

sub open_file {
  local ($filevar, $filemode, $filename) = @_;
  open ($filevar, $filemode . $filename) ||
    die ("Can't open $filename");
}
sub read_from_file {
  local ($filevar) = @_;
  <$filevar>;
}

print "</body>";

